/** 
  *@CopyRight(c)2009 company Co.Ltd. 
  *@All right reserved.
  *@module name: App.BaseInfoManage.OrganManage
  *@description:
  *
  *@author: lugj
  *@version: 1.0
  *@date: 2009/06/30
  *@last date: 2009/06/30
  *@history:
    1.
    2.
  *@bugs:
    1. 
    2. 
  *
*/ 

Ext.namespace("App.BaseInfoManage");

//OrganManage Class
//config: {id: , }
App.BaseInfoManage.OrganManage = function(config){
	this.config = config;
	this.MODULE_ID = App.Base.Common.func.generateGlobalUniqueId(this.config, 'ORGMGR');

	this.OpCmd = App.Base.common.param.OP_CMD_NONE;

	this.waitTreeOpLoadMask = new Ext.LoadMask(Ext.getBody(), {msg: "系统正在加载组织机构信息，请稍候...", removeMask: false});
	this.waitOpLoadMask = new Ext.LoadMask(Ext.getBody(), {msg: "系统正在请求服务器操作，请稍候...", removeMask: false});

	//
	this.handlerReqSuccess = function(response){
		this.waitOpLoadMask.hide();
		//var result = decodeURIComponent(response.responseText);
		var result = Ext.decode(response.responseText);
		//{returnCode: , msg: );
		
		App.Base.Common.func.MESSAGEBOX_SHOW({
			title: App.Base.common.param.MESSAGEBOX_NORMAL_TIPS,
			//msg: result.returnCode + '：'+ result.msg,
			msg: result.msg,
			icon: (result.returnCode == App.Base.common.param.REQ_SUCCESS) ? Ext.MessageBox.INFO:Ext.MessageBox.ERROR
		});
		
		if (result.returnCode == App.Base.common.param.REQ_SUCCESS)
		{
			if (this.OpCmd == App.Base.common.param.OP_CMD_ADD)
			{
				this.tree.getNodeById(this.organParentId + this.MODULE_ID).reload();
			}
			else if (this.OpCmd == App.Base.common.param.OP_CMD_MODIFY)
			{
				this.tree.getNodeById(this.organId + this.MODULE_ID).remove();
				this.tree.getNodeById(this.organParentId + this.MODULE_ID).reload();
			}
			else if (this.OpCmd == App.Base.common.param.OP_CMD_DELETE)
			{
				this.tree.getNodeById(this.organId + this.MODULE_ID).remove();
			}
		}
		//
		this.formPanel.getForm().reset();
		this.OpCmd = App.Base.common.param.OP_CMD_NONE;
	};

	this.handlerReqFailure = function(){
		this.waitOpLoadMask.hide();
		App.Base.Common.func.MESSAGEBOX_SHOW({
			title: App.Base.common.param.MESSAGEBOX_NORMAL_TIPS,
			msg: '服务器请求操作失败，请稍候重试',
			icon: Ext.MessageBox.ERROR
		});	
	};

	this.doRequest = function(config){
		if (config.params != null)
		{
			this.waitOpLoadMask.show();
			
			App.Base.Common.func.request({
				method: 'POST',
				url: App.Base.common.param.ORGMGR_OPERATION_DATAURL,
				success: this.handlerReqSuccess,
				failure: this.handlerReqFailure,
				scope: this,
				params: config.params
			});
		}
		else
		{
			this.OpCmd = App.Base.common.param.OP_CMD_NONE;
			//this.formPanel.getForm().reset();
		}
	};

	this.getParams = function(){

		var organName = Ext.getCmp('organName' + this.MODULE_ID).getValue();
		var organDesc = Ext.getCmp('organDesc' + this.MODULE_ID).getValue();
		var normalImgUrl = Ext.getCmp('normalImgUrl' + this.MODULE_ID).getValue();
		var leafNodeFlag = Ext.getCmp('leafNodeFlag' + this.MODULE_ID).getValue() == true ? '1' : '0';
		var expandImgUrl = Ext.getCmp('expandImgUrl' + this.MODULE_ID).getValue();
		var displayRankingIndex = Ext.getCmp('displayRankingIndex' + this.MODULE_ID).getValue();
		
		if (!this.formPanel.getForm().isValid())
		{
			App.Base.Common.func.MESSAGEBOX_SHOW({
				title: App.Base.common.param.MESSAGEBOX_NORMAL_TIPS,
				msg: '数据格式填写错误',
				icon: Ext.MessageBox.ERROR
			});		
			return null;
		}

		if (Ext.getCmp('organParentName' + this.MODULE_ID).getValue().trim().length == 0)
		{
			App.Base.Common.func.MESSAGEBOX_SHOW({
				title: App.Base.common.param.MESSAGEBOX_NORMAL_TIPS,
				msg: '上级组织机构填写错误',
				icon: Ext.MessageBox.ERROR
			});		
			return null;
		}

		if (leafNodeFlag == '0' && expandImgUrl.trim().length == 0)
		{
			App.Base.Common.func.MESSAGEBOX_SHOW({
				title: App.Base.common.param.MESSAGEBOX_NORMAL_TIPS,
				msg: '目录树展开时的图标URL填写错误',
				icon: Ext.MessageBox.ERROR
			});		
			return null;
		}

		if (this.OpCmd == App.Base.common.param.OP_CMD_MODIFY)
		{
			if (this.organId == this.organParentId)
			{
				App.Base.Common.func.MESSAGEBOX_SHOW({
					title: App.Base.common.param.MESSAGEBOX_NORMAL_TIPS,
					msg: '上级组织机构设置错误',
					icon: Ext.MessageBox.ERROR
				});		
				return null;
			}
		}

		if (this.organId == App.Base.common.param.ORGMGR_TREE_ROOTNODE_ID)
		{
			App.Base.Common.func.MESSAGEBOX_SHOW({
				title: App.Base.common.param.MESSAGEBOX_NORMAL_TIPS,
				msg: '系统不允许操作根目录',
				icon: Ext.MessageBox.ERROR
			});	
			this.formPanel.getForm().reset();
			return;
		}

		return (
			'SYS_OP_CMD=' + encodeURIComponent(this.OpCmd) 
			+ '&ORGAN_ID=' + encodeURIComponent(this.organId)
			+ '&ORGAN_PARENT_ID=' + encodeURIComponent(this.organParentId)
			+ '&ORGAN_NAME=' + encodeURIComponent(organName)
			+ '&ORGAN_DESC=' + encodeURIComponent(organDesc)
			+ '&NORMAL_IMG_URL=' + encodeURIComponent(normalImgUrl)
			+ '&EXPAND_IMG_URL=' + encodeURIComponent(expandImgUrl)
			+ '&LEAF_NODE_FLAG=' + encodeURIComponent(leafNodeFlag)
			+ '&DISPLAY_RANKING_INDEX=' + encodeURIComponent(displayRankingIndex)
		);
	};

	this.doAdd = function(){
		this.doRequest({params: this.getParams()});
	};

	this.doModify = function(){
		this.doRequest({params: this.getParams()});
	};

	this.doDelete = function(node){

		var node = this.tree.getSelectionModel().getSelectedNode();

		if (node == null)
		{
			App.Base.Common.func.MESSAGEBOX_SHOW({
				title: App.Base.common.param.MESSAGEBOX_NORMAL_TIPS,
				msg: '请选择删除的组织机构',
				icon: Ext.MessageBox.ERROR
			});		
			return;
		}

		if (node.id == App.Base.common.param.ORGMGR_TREE_ROOTNODE_ID + this.MODULE_ID)
		{
			App.Base.Common.func.MESSAGEBOX_SHOW({
				title: App.Base.common.param.MESSAGEBOX_NORMAL_TIPS,
				msg: '系统不允许操作根目录',
				icon: Ext.MessageBox.ERROR
			});	
			this.formPanel.getForm().reset();
			return;
		}

		if (node.hasChildNodes())
		{
			App.Base.Common.func.MESSAGEBOX_SHOW({
				title: App.Base.common.param.MESSAGEBOX_NORMAL_TIPS,
				msg: '系统不允许删除有子节点的组织机构',
				icon: Ext.MessageBox.ERROR
			});		
			return;
		}

		Ext.MessageBox.show({
			title: App.Base.common.param.MESSAGEBOX_NORMAL_TIPS,
			msg: '确定要删除组织机构 [' + node.text + ']',
			buttons: Ext.MessageBox.YESNO,
			icon: Ext.MessageBox.QUESTION,
			fn: function(btn){
				if (btn == 'yes')
				{
					this.OpCmd = App.Base.common.param.OP_CMD_DELETE;

					this.organId = node.id.substr(0, node.id.length - this.MODULE_ID.length);

					var params = 'SYS_OP_CMD=' + encodeURIComponent(this.OpCmd) 
						+ '&ORGAN_ID=' + encodeURIComponent(this.organId);

					this.doRequest({params: params});
				}
			},
			scope: this
		});
	};

	this.init = function(node){
		this.organId = node.id.substr(0, node.id.length - this.MODULE_ID.length);

		if (this.organId == App.Base.common.param.ORGMGR_TREE_ROOTNODE_ID)
		{
			App.Base.Common.func.MESSAGEBOX_SHOW({
				title: App.Base.common.param.MESSAGEBOX_NORMAL_TIPS,
				msg: '系统不允许操作根目录',
				icon: Ext.MessageBox.ERROR
			});	
			this.formPanel.getForm().reset();
			return;
		}
		this.organParentId = node.parentNode.id.substr(0, node.parentNode.id.length - this.MODULE_ID.length);
		this.savedOrganParentId = this.organParentId;

		Ext.getCmp('organName' + this.MODULE_ID).setValue(node.text);
		Ext.getCmp('organParentName' + this.MODULE_ID).setValue(node.parentNode.text + App.Base.common.param.SPLIT_CHARS_UNDELINE + this.organParentId);
		Ext.getCmp('organDesc' + this.MODULE_ID).setValue(node.attributes['qtip']);
		Ext.getCmp('normalImgUrl' + this.MODULE_ID).setValue(node.attributes['icon']);
		if (node.isLeaf())
		{
			Ext.getCmp('leafNodeFlag' + this.MODULE_ID).setValue(true);
			Ext.getCmp('expandImgUrl' + this.MODULE_ID).setValue('');
		}
		else
		{
			Ext.getCmp('leafNodeFlag' + this.MODULE_ID).setValue(false);
			Ext.getCmp('expandImgUrl' + this.MODULE_ID).setValue(node.attributes['expandIcon']);
		}
		Ext.getCmp('displayRankingIndex' + this.MODULE_ID).setValue(node.attributes['displayRankingIndex']);
	};

	//tree
	this.asyncTreeNode = new Ext.tree.AsyncTreeNode({
		id: App.Base.common.param.ORGMGR_TREE_ROOTNODE_ID + this.MODULE_ID,
		text: App.Base.common.param.ORGMGR_TREE_ROOTNODE_NORMAL_TITLE,
		icon: App.Base.common.param.ORGMGR_TREE_ROOTNODE_NORMALICON_URL,
		expandIcon: App.Base.common.param.ORGMGR_TREE_ROOTNODE_EXPANDICON_URL,
		qtip: App.Base.common.param.ORGMGR_TREE_ROOTNODE_QTIP
	});
	
	this.tree = new Ext.tree.TreePanel({
		id: 'tree' + this.MODULE_ID,
		autoScroll: true,
		root: this.asyncTreeNode,
		loader: new Ext.tree.TreeLoader({
			dataUrl: App.Base.common.param.ORGMGR_ASYNC_TREELOADER_DATAURL,
			baseParams: {
				module: this.MODULE_ID
			}
		}),
		listeners: {
			beforeexpandnode: function(node){
				node.select();
				node.setIcon(node.attributes['expandIcon']);				
			},
			beforecollapsenode: function(node){
				node.select();
				node.setIcon(node.attributes['icon']);				
			},
			beforeload: function(node){
				this.waitTreeOpLoadMask.show();
			},
			load: function(node){
				this.waitTreeOpLoadMask.hide();
			},
			click: function(node, e){
				this.init(node);
			},
			scope: this
		}
	});

	this.asyncTreeNode.expand();

    this.formPanel = new Ext.FormPanel({
		id: 'formPanel' + this.MODULE_ID,
		autoDestroy: true,
		border: false,
		layout: 'border',
		items: [{
			region:'west',
			title : '组织机构目录树',
			width : 240,
			minWidth: 220,
			autoScroll: true,
			split : true,
			collapsible : true,
			bodyStyle:'padding:5px 5px 5px 0',
			layout: 'fit',
			items: [this.tree]
		},{
			region:'center',
			frame: true,
			border: false,
			autoScroll: true,

			defaults: {width: 620},
			defaultType: 'textfield',

			labelAlign: 'top',
			labelWidth: 128,
			bodyStyle:'padding:5px 5px 5px 5px',

			tbar: new Ext.Toolbar({
				border: false,
				items : ['->', '-', {
					text:'<font color="#0000E3">新增</font>',
					iconCls: 'x-menu-personinfolist-add',
					//tooltip: {title:'Tip Title', text:'This is a an example QuickTip for a toolbar item'},
					//enableToggle: true,
					handler: function(){
						this.OpCmd = App.Base.common.param.OP_CMD_ADD;
						this.doAdd();
					},
					scope: this
				}, '-', {
					text:'<font color="#0000E3">修改</font>',
					iconCls: 'x-menu-systemmenusmanage-modify',
					handler: function(){
						this.OpCmd = App.Base.common.param.OP_CMD_MODIFY;
						this.doModify();
					},
					scope: this
				}, '-', {
					text:'<font color="#0000E3">删除</font>',
					iconCls: 'x-menu-personinfolist-delete',
					handler: function(){
						this.doDelete();
					},
					scope: this
				}, {
					text:'<font color="#0000E3">重置</font>',
					iconCls: 'x-menu-systemmenusmanage-rset',
					handler: function(){
						this.OpCmd = App.Base.common.param.OP_CMD_NONE;
						this.formPanel.getForm().reset();
						},
					scope: this
				}, '-'
				]
			}),
			layout: 'form',
			items :[{
				id: 'organName' + this.MODULE_ID,
				name: 'organName' + this.MODULE_ID,
				fieldLabel: '<font color="red">组织机构名称(必填)</font>',
				maskRe: eval(App.Base.common.param.TEXTFIELD_INPUT_MASKRE),
				regex: eval(App.Base.common.param.TEXTFIELD_INPUT_REGEX),
				regexText: App.Base.common.param.TEXTFIELD_INPUT_REGEX_TIPS,
				allowBlank: false
			},
			
			new Ext.Panel({
				layout:'column',
				items: [{
					width: 440,
					layout: 'form',
					items:[{
						xtype: 'textfield',
						id: 'organParentName' + this.MODULE_ID,
						name: 'organParentName' + this.MODULE_ID,
						width: 420,
						fieldLabel: '<font color="red">上级组织机构ID' + App.Base.common.param.SPLIT_CHARS_UNDELINE + '名称(必填)</font>',
						readOnly: true,
						disabled: true,
						hideLabel: true,
						maskRe: eval(App.Base.common.param.TEXTFIELD_INPUT_MASKRE),
						regex: eval(App.Base.common.param.TEXTFIELD_INPUT_REGEX),
						regexText: App.Base.common.param.TEXTFIELD_INPUT_REGEX_TIPS,
						allowBlank: false,
						emptyText : '请选择上级组织机构...'
					}]
				},{
					width: 160,
					bodyStyle:'padding:5px 5px 5px 0',
					items: [
						new Ext.Button({
							id: 'btnSelectParentId' + this.MODULE_ID,
							text: '<font color="red">选择上级组织机构(必选)</font>',

							handler: function(){
								new App.Base.SystemManage.SystemSelectTreeWindow({
									id: this.MODULE_ID,
									title: '选择上级组织机构',
									leaf: '0',
									asyncTreeNode: {
										id: App.Base.common.param.ORGMGR_TREE_ROOTNODE_ID,
										text: App.Base.common.param.ORGMGR_TREE_ROOTNODE_NORMAL_TITLE,
										icon: App.Base.common.param.ORGMGR_TREE_ROOTNODE_NORMALICON_URL,
										expandIcon: App.Base.common.param.ORGMGR_TREE_ROOTNODE_EXPANDICON_URL,
										qtip: App.Base.common.param.ORGMGR_TREE_ROOTNODE_QTIP,
										singleClickExpand: true
									},
									dataUrl: App.Base.common.param.ORGMGR_ASYNC_TREELOADER_DATAURL,
									handler: function(o, node){
										if (o.action == App.Base.common.param.BUTTON_ACTION_OK)
										{
											var nodeId = node.id.substr(0, node.id.length - o.MODULE_ID.length);
											this.organParentId = nodeId;
											Ext.getCmp('organParentName' + this.MODULE_ID).setValue(node.text + App.Base.common.param.SPLIT_CHARS_UNDELINE + nodeId);
										}
									},
									scope: this
								}).getMainPanel().show(Ext.getCmp('btnSelectParentId' + this.MODULE_ID));
							},
							scope: this
						})	
					]
				}]
			})
			, {
				id: 'organDesc' + this.MODULE_ID,
				name: 'organDesc' + this.MODULE_ID,
				fieldLabel: '<font color="red">组织机构描述</font>',
				maskRe: eval(App.Base.common.param.TEXTFIELD_INPUT_MASKRE),
				regex: eval(App.Base.common.param.TEXTFIELD_INPUT_REGEX),
				regexText: App.Base.common.param.TEXTFIELD_INPUT_REGEX_TIPS,
				allowBlank: false
			}, {
				xtype: 'fieldset',
				title: '组织机构创建为部门',
				collapsible: true,
				collapsed: false,
				autoHeight: true,
				
				labelAlign: 'top',
				labelWidth: 128,
				layout: 'form',
				items: [{
					xtype: 'checkbox',
					id: 'leafNodeFlag' + this.MODULE_ID,
					name: 'leafNodeFlag' + this.MODULE_ID,
					hideLabel: true,
					fieldLabel: '请选择',
					boxLabel: '<font color="red">创建为部门</font>',
					listeners: {
						check: function(checkbox, checked){
							var o = Ext.getCmp('expandImgUrl' + this.MODULE_ID);
							if (checked)
							{
								o.setDisabled(true);
								o.setRawValue('');
							}
							else 
							{
								o.setDisabled(false);
							}
						},
						scope: this
					}
				}, {
					xtype: 'textfield',
					id: 'expandImgUrl' + this.MODULE_ID,
					name: 'expandImgUrl' + this.MODULE_ID,
					width: 596,
					fieldLabel: '<font color="#FF0000">组织机构节点在目录树展开时的图标URL路径(如果是创建组织机构节点，必填)</font>',
					maskRe: eval(App.Base.common.param.TEXTFIELD_INPUT_MASKRE),
					regex: eval(App.Base.common.param.TEXTFIELD_INPUT_REGEX),
					regexText: App.Base.common.param.TEXTFIELD_INPUT_REGEX_TIPS
				}]
			}, {
				xtype: 'fieldset',
				title: '组织机构配置',
				collapsible: true,
				collapsed: false,
				autoHeight: true,
				labelAlign: 'top',
				labelWidth: 128,
				layout: 'form',
				items: [{
					xtype: 'textfield',
					id: 'normalImgUrl' + this.MODULE_ID,
					name: 'normalImgUrl' + this.MODULE_ID,
					width: 596,
					fieldLabel: '<font color="#FF0000">组织机构在目录树中显示的正常图标URL路径(必填)</font>',
					maskRe: eval(App.Base.common.param.TEXTFIELD_INPUT_MASKRE),
					regex: eval(App.Base.common.param.TEXTFIELD_INPUT_REGEX),
					regexText: App.Base.common.param.TEXTFIELD_INPUT_REGEX_TIPS,
					allowBlank: false
				}, 
				/*new Ext.form.DateField({
					id: 'effectDate' + this.MODULE_ID,
					format: 'Y-m-d',
					fieldLabel: '<font color="blue">生效日期</font>',
					width: 128
				}),*/{
					xtype: 'textfield',
					id: 'displayRankingIndex' + this.MODULE_ID,
					name: 'displayRankingIndex' + this.MODULE_ID,
					value: '0',
					width: 596,
					fieldLabel: '<font color="#FF0000">显示排序序号(必填 降序排列)</font>',
					regex: /^-?\d+$/,
					regexText:"只能够输入数字(最多16位)", 
					maxLength: 16, 
					allowBlank: false
				}]
			}]
		}]
	});

	this.formPanel.doLayout();

	//defined methods.
    if((typeof App.BaseInfoManage.OrganManage.SIMULATE_SHARED_METHODS_FLAG) == "undefined")
    {
        App.BaseInfoManage.OrganManage.SIMULATE_SHARED_METHODS_FLAG = true;   

        App.BaseInfoManage.OrganManage.prototype.getMainPanel = function(){   
            return this.formPanel;  
        };
 
        //simulating the class's constructor().
        var __ctor = function(){
            //alert('called __constructor() ok.');
        }();

    }
}